// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Xml.Serialization;

namespace BlueOnionSoftware.Bloget
{
    public class Comment
    {
        private Guid _id = Guid.NewGuid();
        private string _title = string.Empty;
        private string _text = string.Empty;
        private string _author = string.Empty;
        private string _email = string.Empty;
        private DateTime _date = DateTime.UtcNow;

        public Comment()
        {
        }

        internal Comment(Comment comment)
        {
            if (comment == null)
            {
                throw new ArgumentNullException("comment");
            }
            _id = comment._id;
            _title = comment._title;
            _date = comment._date;
            _author = comment._author;
            _email = comment._email;
            _text = comment._text;
        }

        [XmlElement("id")]
        public Guid Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [XmlElement("title")]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        [XmlElement("date")]
        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }

        [XmlElement("author")]
        public string Author
        {
            get { return _author; }
            set { _author = value; }
        }

        [XmlElement("email")]
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        [XmlElement("text")]
        public string Text
        {
            get { return _text ?? string.Empty; }
            set { _text = value; }
        }

        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, "Title: {0}{4}Author: {1}{4}Email: {2}{4}{4}{3}",
                                 Title, Author, Email, Text, Environment.NewLine);
        }

        public bool VerifyComment()
        {
            return (Text.Contains("href=") == false);
        }
    }

    public class CommentCollection : KeyedCollection<Guid, Comment>
    {
        public CommentCollection()
        {
        }

        internal CommentCollection(IEnumerable<Comment> commentCollection)
        {
            if (commentCollection != null)
            {
                foreach (var comment in commentCollection)
                {
                    Add(new Comment(comment));
                }
            }
        }

        protected override Guid GetKeyForItem(Comment item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            return item.Id;
        }
    }
}